package com.huaxin.device.mapping;

import com.huaxin.device.dto.enums.CoolingMethod;
import com.huaxin.device.dto.enums.InsulationLevel;
import com.huaxin.device.dto.request.EquipmentModelCreateReqDTO;
import com.huaxin.device.dto.request.EquipmentModelUpdateReqDTO;
import com.huaxin.device.models.EquipmentModel;
import org.mapstruct.AfterMapping;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.MappingTarget;

@Mapper(componentModel = "spring")
public interface EquipmentModelMapping {

    @Mapping(target = "coolingMethod", ignore = true)
    @Mapping(target = "insulationLevel", ignore = true)
    EquipmentModel toEntity(EquipmentModelCreateReqDTO in);

    @AfterMapping
    default void afterMapping(EquipmentModelCreateReqDTO in, @MappingTarget EquipmentModel entity) {
        if (in.getCoolingMethod() != null) {
            entity.setCoolingMethod(CoolingMethod.of(in.getCoolingMethod()));
        }
        if (in.getInsulationLevel() != null) {
            entity.setInsulationLevel(InsulationLevel.of(in.getInsulationLevel()));
        }
    }

    @Mapping(target = "coolingMethod", ignore = true)
    @Mapping(target = "insulationLevel", ignore = true)
    void updateEntity(EquipmentModelUpdateReqDTO in, @MappingTarget EquipmentModel entity);

    @AfterMapping
    default void afterMapping(EquipmentModelUpdateReqDTO in, @MappingTarget EquipmentModel entity) {
        if (in.getCoolingMethod() != null) {
            entity.setCoolingMethod(CoolingMethod.of(in.getCoolingMethod()));
        }
        if (in.getInsulationLevel() != null) {
            entity.setInsulationLevel(InsulationLevel.of(in.getInsulationLevel()));
        }
    }
}